﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using CompanyBussiness;

namespace Website.customer.Control
{
    public partial class cDownload : System.Web.UI.UserControl
    {
        ProductManagement proMana = new ProductManagement();
        LanguagesManagement languageMana = new LanguagesManagement();
        ServicesBasic servicesBasic = new ServicesBasic();
        CompanyBussiness.Product pro = new Product();
        CompanyBussiness.Language language;
        CompanyBussiness.TypeProperty typeProperty;
        CompanyBussiness.Property property;
        int productIDs;
        string modulCode;
        HtmlMeta metaTag;
        public int CurPage = 1;
        int TotalRow;
        int TotalPage;
        int StartRow = 0;
        NewsManagement newsMana = new NewsManagement();
        CompanyBussiness.New news = new New();
        Metadata meta;

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadDataList();

            metaTag = new HtmlMeta();
            metaTag.Name = "keywords";
            metaTag.Content = "hem";
            Page.Header.Controls.Add(metaTag);

            metaTag = new HtmlMeta();
            metaTag.Name = "description";
            metaTag.Content = "download";
            Page.Header.Controls.Add(metaTag);

            Page.Header.Title = meta.Title;
        }

        protected void LoadDataList()
        { 
            if (Request["Page"] == null)
                {
                    CurPage = 1;
                }
                else
                {
                    CurPage = Convert.ToInt32(Request["Page"]);
                }

                if (CurPage > 1)
                {
                    StartRow = (CurPage - 1) * 10;
                }
                else
                {
                    StartRow = 0;
                }

                dataListFile.DataSource = servicesBasic.GetListFileAttach(10, StartRow);
                dataListFile.DataBind();
                TotalRow = servicesBasic.GetListFileAttachPageCount();

                if (TotalRow == 0)
                {
                    lnkNext.Visible = false;
                    lnkPrev.Visible = false;
                    lnkNext.Text = "";
                    lnkPrev.Text = "";
                    lbNameCategory.Text = languageMana.GetTagValueByLanguageIDandTagName("NotfoundNews", language.LanguagesID);
                    return;
                }
                else
                {
                    lnkNext.Visible = true;
                    lnkPrev.Visible = true;
                }

                this.Pagination(10);
        }


        protected void Pagination(int take)
        {
            try
            {
               

                if (TotalRow % take == 0)
                    TotalPage = TotalRow / take;
                else
                    TotalPage = (TotalRow / take) + 1;



                if (CurPage > 1)
                {


                    lnkPrev.NavigateUrl = "~/Hem/" + (CurPage - 1) + "/Download.htm";
                }
                if (CurPage < TotalPage && TotalPage > 1)
                {


                    lnkNext.NavigateUrl = "~/Hem/" + (CurPage + 1) + "/Download.htm";
                }


                if (CurPage == 1 && TotalPage == 1)
                {
                    lnkNext.Text = "";
                    lnkPrev.Text = "";
                    lnkNext.Visible = false;
                    lnkPrev.Visible = false;
                }
                else if (CurPage == 1 && TotalPage > 1)
                {
                    lnkPrev.Text = "";
                    lnkPrev.Visible = false;
                }
                else if (1 < CurPage && CurPage < TotalPage)
                {

                }
                else if (CurPage == TotalPage)
                {
                    lnkNext.Text = "";
                    lnkNext.Visible = false;
                }
            }
            catch (Exception)
            {

            }
        }

        protected void dataListFile_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item ||
           e.Item.ItemType == ListItemType.AlternatingItem)
            {

                object obj = e.Item.DataItem;

                var name = obj.GetType().GetProperty("Name").GetValue(obj, null);
                var link = obj.GetType().GetProperty("Link").GetValue(obj, null);

                Literal LinkTitle = (Literal)e.Item.FindControl("LinkTitle");
                Button btnViewFile = (Button)e.Item.FindControl("btnViewFile");
                HyperLink fileAttach = (HyperLink)e.Item.FindControl("fileAttach");

                LinkTitle.Text = name.ToString();

                
                fileAttach.NavigateUrl = Server.MapPath(pro.FileAttach);
                //fileAttach.Text = Path.GetFileName(Server.MapPath(pro.FileAttach));


                //Prepare a script to open the file in a new window
                string popupScript = "Javascript: return " +
                "window.open('" + Server.MapPath(link.ToString()) + "', 'CustomPopUp', " +
                "'width=900, height=600, menubar=no, resizable=yes');";


                //Attach Script to button control
                btnViewFile.Attributes.Add("onclick", popupScript);

                
            }
        }


    }
}